package com.grademaster.controller;


import com.grademaster.common.R;
import com.grademaster.dto.UserDto;
import com.grademaster.dto.UserPageDto;
import com.grademaster.entity.User;
import com.grademaster.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

@Slf4j
@RestController
@RequestMapping("/user")
@Api(tags = "用户相关接口")
@CrossOrigin(origins = {"http://localhost:8081"}, allowCredentials = "true")
public class UsersController {

    @Autowired
    private UserService userService;

    @ApiOperation("登录")
    @PostMapping("/login")
    public R login(@RequestBody UserDto userDto){
        return userService.login(userDto);
    }

    @ApiOperation("获取用户权限")
    @GetMapping("/permissions")
    public R getPermissions(){
        return userService.getPermissions();
    }

    @ApiOperation("退出登录")
    @PostMapping("/logout")
    public R logout(){
        return userService.logout();
    }


    @ApiOperation("获取所有用户分页信息")
    @GetMapping("/getAllUsersByPage")
    public R getAllUsersByPage(UserPageDto userPageDto) {
        return userService.getAllUsersByPage(userPageDto);
    }
    @ApiOperation("导出成绩列表到Excel")
    @GetMapping("/exportExcel")
    public ResponseEntity exportExcel(UserPageDto userPageDto){
        return userService.exportExcel(userPageDto);
    }

    @ApiOperation("添加或修改用户信息")
    @PostMapping("/addOrEditUser")
    public R addOrEditUser(@RequestBody User user) {
        return userService.addOrEditUser(user);
    }

    @ApiOperation("删除用户")
    @DeleteMapping("/deleteUser/{userId}")
    public R deleteUser(@PathVariable("userId") String userId) {
        return userService.deleteUser(userId);
    }

    @ApiOperation("获取所有教师信息")
    @GetMapping("/getAllTeachers")
    public R getAllTeachers() {
        return userService.getAllTeachers();
    }

    @ApiOperation("获取用户个人信息")
    @GetMapping("/getUserInfo")
    public R getUserInfo() {
        return userService.getUserInfo();
    }

    @ApiOperation("修改密码")
    @DeleteMapping("/changePassword/{password}")
    public R changePassword(@PathVariable("password") String password){
        return userService.changePassword(password);
    }

}
